<?php
/*
Template Name: Contact Form
*/
?>

<?php // If the form is submitted
if(isset($_POST['submitted'])) {
	// Check to see if the honeypot catcha field was filled in
	if(trim($_POST['checking']) !== '') {
		$captchaError = true;
	} else {
		
		// Check to make sure that the name field is not empty
		if(trim($_POST['contactName']) === '') {
			$nameError = 'You forgot to enter your name.';
			$hasError = true;
		} else {
			$name = trim($_POST['contactName']);
		}
	
		// Check to make sure that a valid email address is submitted
		if(trim($_POST['email']) === '') {
			$emailError = 'You forgot to enter your email address.';
			$hasError = true;
		} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z]{2,4}$", trim($_POST['email']))) {
			$emailError = 'You entered an invalid email address.';
			$hasError = true;
		}

		// Check to make sure comments were entered
		if (trim($_POST['comments']) === '') {
			$commentError = 'You forgot to enter your comments.';
			$hasError = true;
		} else {
			if(function_exists('stripslashes')) {
				$comments = stripslashes(trim($_POST['comments']));
			} else {
				$comments = trim($_POST['comments']);
			}
		}

		// if there is no error send the email
		if(!isset($hasError)) {
			if ($_POST['contactTo'] == 'general') {
				$emailTo = 'nswriters2012@gmail.com';
			}	
			else if ($_POST['contactTo'] == 'webProblems') {
				$emailTo = 'webmaster@spartanspeak.ca';
			}
			else if ($_POST['contactTo'] == 'support') {
				$emailTo = 'support@spartanspeak.ca';
			}
			$subject = 'Contact From Submission From '.$name;
			$sendCopy = trim($_POST['sendCopy']);
			$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
			$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
	
			mail($emailto, $subject, $body, $headers);
	
			if ($sendCopy == true) {
				$subject = 'You emailed Spartans Speak Magazine';
				$headers = 'From: NS Writers <nswriters2012@gmail.com>';
				email($email, $subject, $body, $headers);
			}
	
			$emailSent = true;
	
		}
	}
} ?>

<?php get_header() ; ?>

<?php
// If the email was sent, show a thank you message
// Otherwise show the form
if(isset($emailSent) && $emailSent == true) { ?>

	<div class="thanks">
		<h1>Thanks, <?php echo $name; ?></h1>
		<p>Your email was successfully sent. We will be in touch soon.</p>
	</div>

<?php } else { ?>
	
	<div id="container">
		<div id="content">
<?php the_post(); ?>

			<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
				<header>
					<h1 class="entry-title"><?php the_title(); ?></h1>
				</header>
				<div class="entry-content">
<?php the_content(); ?>

<?php if(isset($hasError) || isset($captchaError)) { ?>
			<p class="error">
				There was an error submitting the form.
			</p>
		<?php } ?>

		<form action="<?php the_permalink(); ?>" id="contactForm" method="post">
			<ol class="forms">
				<li>
					<label for="contactTo">To</label>
					<select name="contactTo">
						<option value="general">General Email</option>
						<option value="webProblems">Webmaster [Website Feedback / Concerns]</option>
						<option value="support">Support [General Concerns]</option>
					</select>
				</li>
				<li>
					<label for="contactName">Name</label>
					<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName']; ?>" class="requiredField" required />
					<?php if($nameError != '') { ?>
						<span class="error"><?php echo $nameError; ?></span>
					<?php } ?>
				</li>
			
				<li>
					<label for="email">Email</label>
					<input type="email" name="email" id="email" value"<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="requiredField email" required />
					<?php if($emailError != '') { ?>
						<span class="error"><?php echo $emailError; ?></span>
					<?php } ?>	
				</li>
			
				<li class="textarea">
					<label for="commentsText">Comments</label>
					<textarea name="comments" id="commentsText" rows="20" cols="30" class="requiredField">
						<?php if(isset($_POST['comments'])) {
							if(function_exists('stripslashes')) {
								echo stripslashes($_POST['comments']);
							} else {
								echo $_POST['comments'];			
							}
							} ?></textarea>
					<?php if($commentError != '') { ?>
						<span class="error"><?php echo $commentError; ?></span>
					<?php } ?>
				</li>
			
				<li class="inline">
					<input type="checkbox" name="sendCopy" id="sendCopy" value="true"<?php if(isset($_POST['sendCopy']) && $_POST['sendCopy'] == true) echo ' checked="checked"'; ?> />
					<label for="sendCopy">Send a copy of this email to yourself</label>
				</li>
			
				<li class="screenReader">
					<label for="checking" class="screenReader">If you want to submit this form, do not enter anything into this field</label>
					<input type="text" name="checking" id="checking" class="screenReader" value="<?php if(isset($_POST['checking'])) echo $_POST['checking']; ?>" />
				</li>
			
				<li class="buttons">
					<input type="hidden" name="submitted" id="submitted" value="true" />
					<button type="submit">Email me &raquo;</button>
				</li>
			</ol>
		</form>
			<footer>
				<?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'spartans-speak' ) . '&after=</div>') ?>
				<?php edit_post_link( __( 'Edit', 'spartans-speak' ), '<span class="edit-link">', '</span>' ) ?>
			</footer>
				</div><!-- .entry-content -->
			</article><!-- #post-<?php the_ID(); ?> -->

<?php if ( get_post_custom_values('comments') ) comments_template() // Add a custom field with Name and Value of "comments" to enable comments on this page ?>

		</div><!-- #content -->
</div><!-- #container -->
<?php } ?>

<?php get_sidebar(); ?>
<?php get_footer(); ?>